<?php
namespace app\services;

use app\models\OrganizationDonates;
use app\models\Organizations;
use app\models\UserDonateRecords;
use app\models\Users;
use think\facade\Db;

class OrganizationService extends ServiceBase
{
    /**
     * @param array $params
     * @param int $page
     * @param int $pagesize
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLists($params=[], $page = 1, $pagesize = 10)
    {
        return Organizations::lists($params, $page, $pagesize);
    }

    public function getDetail($id)
    {
        $organization = Organizations::find($id);

        if ($organization instanceof Organizations) {
            return $organization;
        }

        throw new \Exception('组织不存在');
    }

    /**
     * 添加组织
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function add($params)
    {
        try {
            $organization = new Organizations();

            foreach ($params as $key => $val) {
                $organization->$key = $val;
            }
            $organization->save();

            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * 更新组织
     * @param Organizations $organization
     * @param $params
     */
    public function update(Organizations $organization, $params)
    {
        foreach ($params as $key => $val) {
            $organization->$key = $val;
        }
        $organization->save();
    }

    /**
     * 上架组织
     * @param Organizations $organization
     */
    public function enable(Organizations $organization)
    {
        $organization->status = 1;
        $organization->save();
    }

    /**
     * 下架组织
     * @param Organizations $organization
     */
    public function disable(Organizations $organization)
    {
        $organization->status = 0;
        $organization->save();
    }

    public function getDonateLists($params=[], $page=1, $pagesize=10)
    {


        return OrganizationDonates::lists($params, $page, $pagesize);
    }
}